package controllers

import (
	"alarmservice/model"
	"fmt"
	"sort"
)

type IndexController struct {
	baseController
}

func (c *IndexController) NextPrepare() {
	c.NeedLogin = true

	c.UseLayout()
}

func (c *IndexController) PageIndex() {
	//获取用户关注的projectId
	//根据Id获取20个数据

	followProjects := make(model.SliceFollowProject, 0)
	//XXX 最多允许一个账户跟随500个项目
	err := model.Orm.Where("user_id=?", c.AdminUser.Id).OrderBy("create_time desc").Limit(500, 0).Find(&followProjects)
	if err != nil {
		c.Abort("500")
		return
	}

	projectIds := followProjects.GetProjectIds()
	warnings := make(model.MapWarning)
	if len(projectIds) > 0 {
		err = model.Orm.In("project_id", projectIds).OrderBy("create_time desc").Limit(20, 0).Find(&warnings)
		if err != nil {
			fmt.Println(err)
			c.Abort("500")
			return
		}
		warningIds := warnings.GetWarningIds()
		sort.Sort(sort.Reverse(model.IdList(warningIds)))
		c.Data["Warnings"] = warnings
		c.Data["WarningIds"] = warningIds
	} else {
		c.Data["Warnings"] = warnings
		c.Data["WarningIds"] = warnings.GetWarningIds()
	}

	projects, err := model.GetProjectFromProjectIdContainer(warnings)
	if err != nil {
		c.Abort("500")
		return
	}
	c.Data["Projects"] = projects

	tracing, err := model.GetTracingFromTracingIdContainer(warnings)
	if err != nil {
		c.Abort("500")
		return
	}
	c.Data["Tracings"] = tracing

	c.TplName = "index.tpl"
}

//
func (c *IndexController) PageHelp() {
	c.Data["Host"] = c.Ctx.Request.Host
	c.TplName = "help.tpl"
}
